package com.shijun.im.utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;

import com.shijun.im.R;

/**
 * Created by zhushijun on 2017/11/2 0002.
 */

public class PopWindowUtils {
    private PopWindowUtils() {
    }

    /**
     * 创建一个空的PopupWindow
     */
    private static PopupWindow pop = null;

    /**
     * 判断PopupWindow是否显示
     *
     * @return
     */
    public static boolean isPopShowing() {
        if (pop != null) {
            return pop.isShowing();
        }
        return false;
    }

    /**
     * 显示PopupWindow
     *
     * @param v       随便传一个您的界面有的View
     * @param popView pop的布局
     * @param width   pop显示的宽度
     * @param height  pop显示的高度
     * @param offX    对于v而言的X偏移量
     * @param offY    对于v而言的Y偏移量
     * @param flag    是否有焦点
     */
    public static void showPopupWindow(final View v, View popView, int width, int height, int offX, int offY, boolean flag) {
        hidePopupWindow();
        // 下面是两种方法得到宽度和高度 getWindow().getDecorView().getWidth()
        pop = new PopupWindow(popView);
        pop.setWidth(width);
        //popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
        pop.setHeight(height);
        // 设置popWindow弹出窗体可点击，这句话必须添加，并且是true
        pop.setFocusable(flag);
        //此处为popwindow 设置背景，同事做到点击外部区域，popwindow消失
        pop.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#99000000")));
        //将window视图显示在v的  指定位置
        pop.showAtLocation(v, Gravity.NO_GRAVITY, offX, offY);
        pop.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {

            }
        });
    }

    /**
     * 显示PopupWindow
     *
     * @param activity
     * @param v        随便传一个您的界面有的View
     * @param popView  pop的布局
     * @param width    pop显示的宽度
     * @param height   pop显示的高度
     * @param offX     对于v而言的X偏移量
     * @param offY     对于v而言的Y偏移量
     * @param flag     是否有焦点
     */
    public static void showPopupWindow(final Activity activity, final View v, View popView, int width, int height, int offX, int offY, boolean flag) {
        hidePopupWindow();
        // 下面是两种方法得到宽度和高度 getWindow().getDecorView().getWidth()
        pop = new PopupWindow(popView);
        pop.setWidth(width);
        //popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
        pop.setHeight(height);
//        pop.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        // 设置popWindow弹出窗体可点击，这句话必须添加，并且是true
        pop.setFocusable(flag);
        //此处为popwindow 设置背景，同事做到点击外部区域，popwindow消失
        pop.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#60000000")));
        //将window视图显示在v的  指定位置

        pop.showAtLocation(v, Gravity.NO_GRAVITY, offX, offY);
        pop.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {

            }
        });
    }

    /**
     * 显示PopupWindow
     *
     * @param v       随便传一个您的界面有的View
     * @param popView pop的布局
     * @param width   pop显示的宽度
     * @param height  pop显示的高度
     * @param offX    对于v而言的X偏移量
     * @param offY    对于v而言的Y偏移量
     * @param flag    是否有焦点
     * @param color   背景颜色
     */
    public static void showPopupWindow(final View v, View popView, int width, int height, int offX, int offY, boolean flag, int color) {
        hidePopupWindow();
        // 下面是两种方法得到宽度和高度 getWindow().getDecorView().getWidth()
        pop = new PopupWindow(popView);
        pop.setWidth(width);
        //popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
        pop.setHeight(height);
        // 设置popWindow弹出窗体可点击，这句话必须添加，并且是true
        pop.setFocusable(flag);
        //此处为popwindow 设置背景，同事做到点击外部区域，popwindow消失
        pop.setBackgroundDrawable(new ColorDrawable(color));
        //将window视图显示在v的  指定位置
        pop.showAtLocation(v, Gravity.NO_GRAVITY, offX, offY);
        pop.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {

            }
        });
    }

    /**
     * 隐藏PopupWindow
     */
    public static void hidePopupWindow() {
        if (pop != null) {
            pop.dismiss();
            pop = null;
        }
    }


    /**
     * @param popView      要显示的view
     * @param relativeView 以该view的位置相对显示,为空就以屏幕位置显示
     * @param activity     activity
     * @param isfocusable  外部是否可点击
     * @param gravity      相对于屏幕的显示位置
     * @param width         popwindow的宽 ,如WindowManager.LayoutParams.WRAP_CONTENT||MATCH_PARENT
     * @param height        popwindow的高
     * @param offX         x轴偏移量
     * @param offY         y轴偏移量
     * @param alpha        外部的透明度参数0.1~1之间
     */
    public static void showPopupWindow(View popView, View relativeView, final Activity activity, boolean isfocusable,
                                       int gravity,int width,int height, int offX, int offY, float alpha) {
        pop = new PopupWindow(popView, width,
                height, isfocusable);
        WindowManager.LayoutParams params = activity.getWindow().getAttributes();//获取系统布局参数
        pop.setBackgroundDrawable(new BitmapDrawable());
        pop.setOutsideTouchable(isfocusable);
        //让键盘弹出时，不会挡住pop窗口。
        pop.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
        pop.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        params.alpha = alpha;//设置透明度参数
        activity.getWindow().setAttributes(params);//设置透明度
        if (relativeView != null) {//如果有,则以该控件位置偏移
            pop.showAsDropDown(relativeView, offX, offY);//显示位置和偏移量

        } else {//如果无,则以屏幕位置偏移
            pop.showAtLocation(popView, gravity, offX, offY);
        }
        pop.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {//pop关闭时监听
                WindowManager.LayoutParams params = activity.getWindow().getAttributes();
                params.alpha = 1.0f;//设置为透明，即恢复原来的界面
                activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
                activity.getWindow().setAttributes(params);//重新设置透明度
                pop.dismiss();//关闭
            }
        });
    }

    /**
     * @param popView      要显示的view
     * @param relativeView 以该view的位置相对显示,为空就以屏幕位置显示
     * @param activity     activity
     * @param isfocusable  外部是否可点击
     * @param gravity      相对于屏幕的显示位置
     * @param offX         x轴偏移量
     * @param offY         y轴偏移量
     * @param alpha        外部的透明度参数0.1~1之间
     */
    public static void showPopupWindow(Context context,View popView, View relativeView, final Activity activity, boolean isfocusable,
                                       int gravity, int offX, int offY, float alpha) {
        pop = new PopupWindow(popView, WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.WRAP_CONTENT, isfocusable);
        WindowManager.LayoutParams params = activity.getWindow().getAttributes();//获取系统布局参数
        pop.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.background_circularbead_white));
        pop.setOutsideTouchable(isfocusable);
        params.alpha = alpha;//设置透明度参数
        activity.getWindow().setAttributes(params);//设置透明度
        if (relativeView != null) {//如果有,则以该控件位置偏移
            pop.showAsDropDown(relativeView, offX, offY);//显示位置和偏移量

        } else {//如果无,则以屏幕位置偏移
            pop.showAtLocation(popView, gravity, offX, offY);
        }
        pop.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {//pop关闭时监听
                WindowManager.LayoutParams params = activity.getWindow().getAttributes();
                params.alpha = 1.0f;//设置为透明，即恢复原来的界面
                activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
                activity.getWindow().setAttributes(params);//重新设置透明度
                pop.dismiss();//关闭
            }
        });
    }

    /**
     * 更新pop高度
     *
     * @param height
     */
    public static void offHeight(int height) {
        if (null != pop) {
            int width = pop.getWidth();
            pop.update(width, height);
        }
    }
}
